package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;

    @RequiredPermission(name = "投票页面", expression = "activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }





    @RequestMapping("/isActivity")
    @ResponseBody
    public JsonResult input(Long id) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = activityService.selectOne(id, UserContext.getEmployee().getId());
        if (integer > 0){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("已填写过");
            return jsonResult;
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    @RequestMapping("/input")
    @RequiredPermission(name = "投票新增", expression = "activity:input")
    public String input(Long id, Model model, HttpSession session) {
        //通过id获取标题
        Activity activity = activityService.get(id);

        //通过id获取所有的Item
        List<ActivityItem> activityItems = activityItemService.selectByActId(id);

        model.addAttribute("activity", activity);
        model.addAttribute("activityItems", activityItems);
        //把投票Id存到session里面
        session.setAttribute("id", id);

        return "/activity/input";
    }






    @RequestMapping("/save")
    @RequiredPermission(name = "投票新增/编辑", expression = "activity:save")
    @ResponseBody
    public JsonResult save(ActivityItem item, Model model) {
        Long id = UserContext.getEmployee().getId();
        Long itemId = item.getId();
        activityService.saveIntermediate(id, itemId, new Date());
        activityItemService.updateOne(itemId);
        return new JsonResult();
    }






    @RequestMapping("/result")
    @RequiredPermission(name = "投票结果", expression = "activity:result")
    public String result(Model model, HttpSession session) {
        //获取数据显示显示在result页面
        Object id1 = session.getAttribute("id");
        Activity activity = activityService.selectResult((Long) id1);
        System.out.println(activity);
        model.addAttribute("activity", activity);
        //获取总数
        List<ActivityItem> activityItem = activity.getActivityItem();
        int sum = 0;
        for (ActivityItem item : activityItem) {
            sum += item.getNum();
        }
        model.addAttribute("sum", sum);
        return "activity/result";
    }

}
